package com.ruoyi.Course.service.impl;

import com.ruoyi.Course.domian.courseDto;
import com.ruoyi.Course.domian.courseStudent;
import com.ruoyi.Course.domian.entity.CourseStudentEntity;
import com.ruoyi.Course.domian.entity.CourseTeacherEntity;
import com.ruoyi.Course.service.courseService;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.teacherCourse.mapper.TeacherCourseMapper;
import com.ruoyi.teacherInformation.domain.Teacher;
import com.ruoyi.teacherInformation.mapper.TeacherMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.Course.domian.courseTime;
import com.ruoyi.Course.mapper.courseMapper;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@Service
@Slf4j
public class courseServiceImpl implements courseService {

    @Autowired
    private courseMapper courseMapper;

    @Override
    @Transactional
    public AjaxResult insertCourse(courseDto courseDto) {
        log.info("courseServiceImpl插入课程："+courseDto);

        //生成课程id
        String uuid = UUID.randomUUID().toString();
        String courseId = "create" + uuid.replace("-", "").substring(0, 16);

        //查一次老师，如果没有老师则返回错误，获取老师系和老师名
        Teacher teacher = courseMapper.selectTeacherByTeacherId(courseDto.getTeacherId());
        if(teacher == null) {
            return AjaxResult.error("老师工号有误");
        }

        //查课表，如果有相同的课程名称和老师就返回错误
        List<CourseTeacherEntity> courseTeacherEntities = courseMapper
                .selectCourseTeacherByCourseNameAndTeacherId(courseDto.getCourseName(), courseDto.getTeacherId());
        if(courseTeacherEntities.size() != 0) {
            return AjaxResult.error("已有该课程,不要重复添加");
        }

        //将学生的arr改为list<courseStudent>,查到学生的所有系，班级，总人数
        List<courseStudent> studentList = courseDto.getStudentList().stream().map(student -> {
            String studentName = student.get(2).replaceAll("[^\\u4e00-\\u9fa5]", "");
            Long studentId = Long.valueOf(student.get(2).replaceAll("[^0-9]", ""));
            return new courseStudent(student.get(0), student.get(1), studentId, studentName);
        }).collect(Collectors.toList());
        Set<String> allDeptsNum = studentList.stream().map(courseStudent::getDept).collect(Collectors.toSet());
        Set<String> allClassNum = studentList.stream().map(courseStudent::getStuClass).collect(Collectors.toSet());
        int allStudentNum = studentList.size();

        //根据上课时间list.MAP
        for(courseTime coursetime : courseDto.getCourseTime()) {
            if(coursetime.getWeekType().equals("不分单双")){coursetime.setWeekType("");}//改回单双空
            CourseTeacherEntity courseTeacherEntity = new CourseTeacherEntity();
            courseTeacherEntity.setCourseId(courseId);
            courseTeacherEntity.setCourseName(courseDto.getCourseName());
            courseTeacherEntity.setTeacherDept(teacher.getDept());
            courseTeacherEntity.setTeacherName(teacher.getName());
            courseTeacherEntity.setTeacherId(courseDto.getTeacherId());
            courseTeacherEntity.setSumCnt(allStudentNum);
            courseTeacherEntity.setSelectCnt(allStudentNum);
            courseTeacherEntity.setClassDept(String.join(",",allDeptsNum));
            courseTeacherEntity.setClasses(String.join(",",allClassNum));
            courseTeacherEntity.setWeek(courseDto.getWeek());
            courseTeacherEntity.setWeekType(coursetime.getWeekType());
            courseTeacherEntity.setPeriod(coursetime.getPeriod());
            courseTeacherEntity.setPlace(courseDto.getPlace());
            courseTeacherEntity.setWeekday(coursetime.getWeekday());
            courseTeacherEntity.setGrop("");

            //添加到老师表
            int i = courseMapper.insertTeacherCourse(courseTeacherEntity);
            if(i == 0) {
                return AjaxResult.error("插入老师表失败");
            }

            //回显老师表id
            Integer courseTeacherId = courseTeacherEntity.getId();

            //添加到学生表
            for (courseStudent student : studentList) {
                CourseStudentEntity courseStudentEntity = new CourseStudentEntity();
                courseStudentEntity.setStudentId(student.getStuId());
                courseStudentEntity.setStudentName(student.getStuName());
                courseStudentEntity.setCourseId(courseId);
                courseStudentEntity.setCourseName(courseDto.getCourseName());
                courseStudentEntity.setTeacherName(teacher.getName());
                courseStudentEntity.setWeek(courseDto.getWeek());
                courseStudentEntity.setWeektype(coursetime.getWeekType());
                courseStudentEntity.setWeekday(coursetime.getWeekday());
                courseStudentEntity.setPeriod(coursetime.getPeriod());
                courseStudentEntity.setPlace(courseDto.getPlace());
                courseStudentEntity.setClassName(student.getStuClass());
                courseStudentEntity.setCourseTeacherId(courseTeacherId);

                int j = courseMapper.insertStudentCourse(courseStudentEntity);
                if (j == 0) {return AjaxResult.error("插入学生表失败");}
            }
        }
        return AjaxResult.success();
    }
}
